﻿using System.Collections.Generic;
using UnityEngine;

namespace UnityView
{
    public class UITableView : AbsAdapterView<IAdapter>
    {
        public UITableView(string name = "Table View") : base(UICanvas.GetInstance(), name)
        {
        }

        public UITableView(IViewLayout parent, string name = "Table View") : base(parent, name)
        {
        }

        protected float itemSize = 100;

        public float ItemSize
        {
            get => itemSize;
            set
            {
                itemSize = value;
                Reload();
            }
        }

        public override void UpdateVisibleItemCount()
        {
            VisibleItemCount = Mathf.CeilToInt(Height / itemSize) + 1;
        }

        protected override int GetStartIndex()
        {
            return GetStartIndex(ContentOffsetWithoutHeader);
        }

        protected int GetStartIndex(float offset)
        {
            if (ItemCount == 0) return 0;
            if (offset < 0) return 0;
            var startIndex = Mathf.CeilToInt(offset / itemSize);
            if (startIndex >= ItemCount) return ItemCount - 1;
            return startIndex;
        }

        public override void Reload()
        {
            // 停止滚动
            ScrollRect.StopMovement();
            if (Adapter == null) return;

            ItemCount = Adapter.GetCount();
            UpdateContentSize();
            StartIndex = GetStartIndex();
            UpdateVisibleItemCount();
            LayoutSubviews();
        }

        /// <summary>
        /// StartIndex 已经改变，需要重新布局元素
        /// </summary>
        public override void LayoutSubviews()
        {
            // var info = CellCache.Inquire(StartIndex, VisibleItemCount);
            // int dirtyIndex = info.DirtyIndex;
            // int dirtyCount = info.DirtyCount;
            // Debug.Log($"VisibleItemCount: {VisibleItemCount}, dirtyIndex: {dirtyIndex}, dirtyCount: {dirtyCount}");
            // var node = CellCache.Current;
            // for (int i = 0; i < dirtyCount; i++)
            // {
            //     AdapterCellHolder holder = node.Value;
            //     int index = dirtyIndex + i;
            //     IViewLayout view = Adapter.GetConvertView(index, holder.View);
            //     view.ParentView = ContentView;
            //     holder.Update(view, index, GetItemPosition(index));
            //     node = node.Next;
            // }
        }

        protected override Vector2 UpdateContentSize()
        {
            ContentSize = new Vector2(Width, itemSize * ItemCount + headerView.Height + footerView.Height);
            return ContentSize;
        }
        
        public override void OnAdapterAttached(IAdapter adapter)
        {
            ItemCount = adapter.GetCount();
            UpdateContentSize();
        }

        public override Vector2 GetItemPosition(int index)
        {
            return Vector2.zero;
        }

        public override int GetItemIndex(Vector2 vector2)
        {
            return 0;
        }
    }

    public class UIHorizontalTableView : AbsAdapterView<IAdapter>
    {
        public override void UpdateVisibleItemCount()
        {
            throw new System.NotImplementedException();
        }

        protected override int GetStartIndex()
        {
            throw new System.NotImplementedException();
        }

        public override void Reload()
        {
            throw new System.NotImplementedException();
        }

        public override void LayoutSubviews()
        {
            throw new System.NotImplementedException();
        }

        protected override Vector2 UpdateContentSize()
        {
            throw new System.NotImplementedException();
        }

        public override void OnAdapterAttached(IAdapter adapter)
        {
            throw new System.NotImplementedException();
        }

        public override Vector2 GetItemPosition(int index)
        {
            throw new System.NotImplementedException();
        }

        public override int GetItemIndex(Vector2 vector2)
        {
            throw new System.NotImplementedException();
        }
    }
}
